package com.ssy.lingxi.member.merchant.repository;

import com.ssy.lingxi.member.merchant.entity.MemberDO;
import com.ssy.lingxi.member.merchant.entity.MemberUserDO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 会员下属用户操作类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-06-28
 */
@Repository
public interface MemberUserRepository extends JpaRepository<MemberUserDO, Long>, JpaSpecificationExecutor<MemberUserDO> {
    Page<MemberUserDO> findByMemberAndAccountContainsAndNameContainsAndStatusIn(MemberDO memberDO, String account, String name, List<Integer> statusList, Pageable pageable);

    List<MemberUserDO> findByMemberAndTypeEnum(MemberDO member, Integer typeEnum);

    MemberUserDO findFirstByMemberAndId(MemberDO memberDO, Long id);

    MemberUserDO findFirstByMemberNotAndPhone(MemberDO memberDO, String phone);

    MemberUserDO findFirstByMemberNotAndEmail(MemberDO memberDO, String email);

    MemberUserDO findFirstByEmailAndRelType(String email, Integer relType);

    MemberUserDO findFirstByMemberAndTypeEnum(MemberDO memberDO, Integer typeEnum);

    @Query(value = "select * from ms_mc_member_user where rel_type = :relType and (account = :account or phone = :phone) limit 1", nativeQuery = true)
    MemberUserDO findLoginUser(String account, String phone, Integer relType);

    MemberUserDO findFirstByPhoneAndRelType(String phone, Integer relType);

    MemberUserDO findFirstByCountryCodeAndPhoneAndRelType(String countryCode, String phone, Integer relType);

    List<MemberUserDO> findByMemberIn(List<MemberDO> memberDOList);

    boolean existsByRelTypeAndEmail(Integer relType, String email);

    boolean existsByRelTypeAndEmailAndIdNot(Integer relType, String email, Long id);

    boolean existsByRelTypeAndPhone(Integer relType, String phone);

    boolean existsByRelTypeAndPhoneAndIdNot(Integer relType, String phone, Long id);
}
